package BloodTimeHybernateTest;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import BloodTimeHybernateDO.VisitaDO;

/**
 * 
 * @author Eduardo D. Sandoval V.
 * 
 */
public class TestVisitaDO {

	public static void main(String[] args) {
		Configuration configuration = new AnnotationConfiguration();
		configuration.configure("/BloodTimeHybernateDO/hibernate.cfg.xml");
		SessionFactory sessionFactory = configuration.buildSessionFactory();

		// ------------------------------
		// Insert
		// ------------------------------

		Session session = sessionFactory.openSession();
		session.beginTransaction();
		for (int i = 0; i < 10; i++) {
			VisitaDO visita = new VisitaDO();
			visita.setProbHordas(i / 10);
			session.save(visita);
		}

		session.getTransaction().commit();
		session.close();

		// ---------------------------------------------------------------
		// Load
		// ---------------------------------------------------------------

		session = sessionFactory.openSession();
		session.beginTransaction();

		VisitaDO visita = new VisitaDO();

		visita = null;
		visita = (VisitaDO) session.load(VisitaDO.class, 1);

		System.out.println("Load Visita");
		System.out.println("Id: " + visita.getId());
		System.out.println("Probabilidad de ser atacado: "
				+ visita.getProbHordas());

		session.getTransaction().commit();
		session.close();
		
		// ---------------------------------------------------------------
        // Query
        // ---------------------------------------------------------------

        session = sessionFactory.openSession();
        session.beginTransaction();

        Query query;

        visita = null;
        query = session.createQuery("FROM VisitaDO WHERE id = :visitaId");
        query.setParameter("visitaId", 1);

        visita = (VisitaDO) query.uniqueResult();

        System.out.println("Query Visita 1");
        System.out.println("Id: " + visita.getId());
        System.out.println("Probabilidad de ser atacado: " + visita.getProbHordas());
        
     // ---------------------------------------------------------------
        // Delete
        // ---------------------------------------------------------------

        session.delete(visita);

        session.getTransaction().commit();
        session.close();

        // ---------------------------------------------------------------
        // List
        // ---------------------------------------------------------------

        session = sessionFactory.openSession();
        session.beginTransaction();

        System.out.println("List Visita");

        List<VisitaDO> list = session.createCriteria(VisitaDO.class).list();
        for (VisitaDO visitaDO : list) {
            System.out.println("Id: " + visitaDO.getId());
            System.out.println("Nombre: " + visitaDO.getProbHordas());

        }

        session.getTransaction().commit();
        session.close();

    }

}
